package com.ljf.authority.domain.service;

import com.ljf.authority.domain.repository.AuthorityBaseMenuTabulationRepository;
import com.ljf.authority.domain.repository.AuthorityDatPermissionGroupRepository;
import com.ljf.authority.domain.repository.AuthorityRelPermissionGroupMenuRepository;
import com.ljf.authority.domain.repository.AuthorityRelPermissionGroupUserRepository;
import com.ljf.authority.dto.command.AuthorityDatPermissionGroupCommand;
import com.ljf.authority.dto.command.AuthorityDatPermissionGroupMenuCommand;
import com.ljf.authority.dto.command.AuthorityDatPermissionGroupUserCommand;
import com.ljf.authority.dto.query.AuthorityBaseMenuTabulationQuery;
import com.ljf.authority.dto.query.AuthorityDatPermissionGroupQuery;
import com.ljf.authority.dto.query.AuthorityRelPermissionGroupMenuQuery;
import com.ljf.authority.dto.query.AuthorityRelPermissionGroupUserQuery;
import com.ljf.authority.model.AuthorityDatPermissionGroup;
import com.ljf.authority.model.AuthorityRelPermissionGroupMenu;
import com.ljf.authority.model.AuthorityRelPermissionGroupUser;
import com.ljf.authority.vo.*;
import com.ljf.common.domain.service.BasisDomainService;
import com.ljf.common.exception.ServiceException;
import com.ljf.common.utils.CollectionUtil;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 权限组  领域服务
 *
 * @author LJF
 * @since 2025-06-08
 */
@Component
@AllArgsConstructor
public class AuthorityDatPermissionGroupDomainService extends BasisDomainService<AuthorityDatPermissionGroupRepository, AuthorityDatPermissionGroupQuery, AuthorityDatPermissionGroupVO, AuthorityDatPermissionGroup, AuthorityDatPermissionGroupCommand> {

    private final AuthorityRelPermissionGroupMenuRepository relPermissionGroupMenuRepository;
    private final AuthorityRelPermissionGroupUserRepository relPermissionGroupUserRepository;
    private final AuthorityBaseMenuTabulationRepository baseMenuTabulationRepository;

    /**
     * 修改权限组菜单
     *
     * @param command
     * @throws ServiceException
     */
    public void editMenu(AuthorityDatPermissionGroupMenuCommand command) throws ServiceException {
        AuthorityDatPermissionGroupVO model = repository.selectById(command.getId());
        relPermissionGroupMenuRepository.physicalDelete(
                AuthorityRelPermissionGroupMenuQuery.builder().groupCode(model.getCode()).build()
        );
        if (CollectionUtil.isNotEmpty(command.getMenuList())) {
            List<AuthorityRelPermissionGroupMenu> needInsertList = new ArrayList<>();
            for (String menuCode : command.getMenuList()) {
                needInsertList.add(
                        AuthorityRelPermissionGroupMenu.builder().groupCode(model.getCode()).menuCode(menuCode).build()
                );
            }
            relPermissionGroupMenuRepository.insertBatch(needInsertList);
        }
    }

    /**
     * 修改权限组用户
     *
     * @param command
     * @throws ServiceException
     */
    public void editUser(AuthorityDatPermissionGroupUserCommand command) throws ServiceException {
        AuthorityDatPermissionGroupVO model = repository.selectById(command.getId());
        relPermissionGroupUserRepository.physicalDelete(
                AuthorityRelPermissionGroupUserQuery.builder().groupCode(model.getCode()).build()
        );
        if (CollectionUtil.isNotEmpty(command.getUserList())) {
            List<AuthorityRelPermissionGroupUser> needInsertList = new ArrayList<>();
            for (String user : command.getUserList()) {
                needInsertList.add(
                        AuthorityRelPermissionGroupUser.builder().groupCode(model.getCode()).user(user).build()
                );
            }
            relPermissionGroupUserRepository.insertBatch(needInsertList);
        }
    }

    /**
     * 查询权限组菜单
     *
     * @param id
     * @return
     * @throws ServiceException
     */
    public AuthorityDatPermissionGroupMenuVO queryMenuById(Long id) throws ServiceException {
        AuthorityDatPermissionGroupVO model = repository.selectById(id);
        List<AuthorityRelPermissionGroupMenuVO> permissionGroupMenuList = relPermissionGroupMenuRepository.selectList(
                AuthorityRelPermissionGroupMenuQuery.builder().groupCode(model.getCode()).build()
        );
        List<AuthorityBaseMenuTabulationVO> menuList = Collections.emptyList();
        if (CollectionUtil.isNotEmpty(permissionGroupMenuList)) {
            List<String> menuCodeList = permissionGroupMenuList.stream()
                    .map(AuthorityRelPermissionGroupMenuVO::getMenuCode).distinct().collect(Collectors.toList());
            menuList = baseMenuTabulationRepository.selectList(AuthorityBaseMenuTabulationQuery.builder().codeList(menuCodeList).build());
        }
        return AuthorityDatPermissionGroupMenuVO.builder()
                .code(model.getCode())
                .name(model.getName())
                .menuList(menuList)
                .build();
    }

    /**
     * 查询权限组用户
     *
     * @param id
     * @return
     * @throws ServiceException
     */
    public AuthorityDatPermissionGroupUserVO queryUserById(Long id) throws ServiceException {
        AuthorityDatPermissionGroupVO model = repository.selectById(id);
        List<AuthorityRelPermissionGroupUserVO> permissionGroupUserList = relPermissionGroupUserRepository.selectList(
                AuthorityRelPermissionGroupUserQuery.builder().groupCode(model.getCode()).build()
        );
        List<String> userList = permissionGroupUserList.stream()
                .map(AuthorityRelPermissionGroupUserVO::getUser).distinct().collect(Collectors.toList());
        return AuthorityDatPermissionGroupUserVO.builder()
                .code(model.getCode())
                .name(model.getName())
                .userList(userList)
                .build();
    }

}
